<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          
          <a-col :span="12">
            <a-form-model-item label="分段序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partIndex">
              <a-input-number v-model="model.partIndex" placeholder="请输入分段序号" style="width: 100%"  @blur="partIndexChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分段类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partType">
              <j-dict-select-tag type="list" v-model="model.partType" dictCode="auto_code_part_type" placeholder="请选择分段类型"  @change="partTypeChange"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="分段编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partCode">
              <a-input v-model="model.partCode" placeholder="请输入分段编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分段名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partName">
              <a-input v-model="model.partName" placeholder="请输入分段名称"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="分段长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partLength">
              <a-input-number v-model="model.partLength" placeholder="请输入分段长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='NOWDATE'">
            <a-form-model-item label="日期格式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dateFormat">
              <j-dict-select-tag type="list" v-model="model.dateFormat" dictCode="date_format" placeholder="请选择日期格式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='INPUTCHAR'">
            <a-form-model-item label="输入字符" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inputCharacter">
              <a-input v-model="model.inputCharacter" placeholder="请输入输入字符"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='FIXCHAR'">
            <a-form-model-item label="固定字符" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fixCharacter">
              <a-input v-model="model.fixCharacter" placeholder="请输入固定字符"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12"  v-show="model.partType=='SERIALNO'">
            <a-form-model-item label="流水号起始值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seriaStartNo">
              <a-input-number v-model="model.seriaStartNo" placeholder="请输入流水号起始值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='SERIALNO'">
            <a-form-model-item label="流水号步长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seriaStep">
              <a-input-number v-model="model.seriaStep" placeholder="请输入流水号步长" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='SERIALNO'">
            <a-form-model-item label="流水号当前值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seriaNowNo">
              <a-input-number v-model="model.seriaNowNo" placeholder="请输入流水号当前值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='SERIALNO'">
            <a-form-model-item label="是否循环" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cycleFlag">
              <j-dict-select-tag type="radio" v-model="model.cycleFlag" dictCode="yes_no" placeholder="请选择流水号是否循环" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.partType=='SERIALNO' && model.cycleFlag=='1'">
            <a-form-model-item label="循环方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cycleMethod">
              <j-dict-select-tag type="list" v-model="model.cycleMethod" dictCode="auto_code_cycle_method" placeholder="请选择循环方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AutoCodePartForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        visible: false,
        ruleId:"",
        validatorRules: {
           partIndex: [
              { required: true, message: '请输入分段序号!'},
           ],
           partType: [
              { required: true, message: '请输入分段类型!'},
           ],
           partLength: [
              { required: true, message: '请输入分段长度!'},
           ],
        },
        url: {
          add: "/system/autoCodePart/add",
          edit: "/system/autoCodePart/edit",
          queryById: "/system/autoCodePart/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      partIndexChange(){
        this.model.partCode="PART_"+this.model.partIndex;
        this.$forceUpdate();
      },
      partTypeChange(){
        this.model.partName=this.model.partIndex+this.model.partType;
        this.$forceUpdate();
      },
      add(ruleId) {
        this.ruleId = ruleId;
        //初始化默认值
        let record={
          ruleId:this.ruleId,
          id:undefined
        }
        this.edit(record);
      },
      edit(record) {
        if (record.ruleId) {
          this.ruleId = record.ruleId;
        }  
        this.model=Object.assign({},record);
        this.model.ruleId = this.ruleId;
        console.log(this.ruleId,record,this.model);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>